package com.dgh.school.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.dgh.school.bean.Duty;
import com.dgh.school.common.R;
import com.dgh.school.mapper.DutyMapper;
import com.dgh.school.service.DutyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DutyServiceImpl implements DutyService {

    @Autowired
    private DutyMapper dutyMapper;

    @Override
    public R selectByPage(int pageNum, int pageSize) throws Exception {

        PageHelper.startPage(pageNum, pageSize);
        // 查询全部数据
        List<Duty> duties = dutyMapper.selectAll();
        // 借助分页助手，获得分页信息
        PageInfo<Duty> pageInfo = new PageInfo<>(duties);

        return R.ok().setDate("pageInfo", pageInfo);
    }

    @Override
    public void deleteById(int id) throws Exception {
        dutyMapper.deleteById(id);
    }

    @Override
    public void insertData(Duty duty) throws Exception {
        dutyMapper.insert(duty);
    }

    @Override
    public void updateData(Duty duty) throws Exception {
        dutyMapper.update(duty);
    }

    @Override
    public R selectAllDutys() throws Exception {
        List<Duty> duties =  dutyMapper.selectAll();

        return R.ok().setDate("duties",duties);
    }
}
